var timer;

//set timeout to prevent multiple popups
function onMOver(i, country, name, link) {
    clearTimeout(timer);
    var obj = document.getElementById('image' + i);
    timer = setTimeout(function () { time(obj, country, name, link, i); }, 1000);
}

//creating of popup form
function time(obj, country, name, link, i) {
    var width = 270;
    var height = 250;

    var mainDimSize = height - 60;
    if (obj == null) {
        return;
    }
    var newDims = initDimensions(obj.width, obj.height, mainDimSize);

    var left;
    var top;

    //if ($.browser.mozilla) {
 
    //}
    left = (obj.parentNode.parentNode.parentNode.clientWidth - width) / 2;
    top = (obj.parentNode.parentNode.parentNode.clientHeight - height) / 2;

    

    var absoluteFuckenPath = "http://localhost:64708/DynamicSearch/";

    var html = '<div class="miniPreview'+ (i % 2) +'"  id="preview" style="border: 1px solid">\n';

    html += '    <div id="dName1">\n';
    html += '    <div id="dImage">\n';
    html += '        <a href="http://bringmirbio.de/shop/' + link + '" id="container" > \n';
    html += '	        <img class="image" src="' + obj.src + '" alt="s" id="previewImage" />\n';
    html += '        </a>\n';
    html += '        <a href="http://bringmirbio.de/shop/' + link + '" id="container2"  > \n';
    html += '	        <img class="image" src="' + obj.src + '" alt="s" id="previewImage2" />\n';
    html += '        </a>\n';
    html += '    </div>\n';
    html += '    <div id="dName">\n';
    html += '        <img class="backImages" src="' + absoluteFuckenPath + 'images/forname.png" />\n';

    html += '        <div id="dName_1">\n';
    html += '	        <div id="dNameTextLarge">' + name + '</div>\n';
    html += '        </div>\n';
    html += '    </div>\n';
    html += '    <div id="dPrice">\n';
    html += '        <img class="backImages" src="' + absoluteFuckenPath + 'images/forprice.png"/>\n';

    html += '        <div id="dBigPriceLarge">2</div>\n';
    html += '        <div id="dAddPrice">\n';
    html += '	        <div id="centsLarge">49</div>\n';
    html += '	        <div class="smthLarge">&euro; pro 700 g</div>\n';
    html += '	        <div class="smthLarge" >1 kg = 4,79 &euro;</div>\n';
    html += '        </div>\n';
    html += '    </div>\n';
    html += '  </div>\n';
    html += '</div>\n';

    $(obj.parentNode.parentNode).after(html);

    $('#preview').css({ 'width': width + 'px', 'height': height + 'px' });
	$('#preview').css({ 'margin-left': left + 'px', 'margin-top': top + 'px' });
	$('#previewImage').css({ 'width': newDims[0] + 'px', 'height': newDims[1] + 'px' });
	$('#previewImage2').css({ 'width': newDims[0] + 'px', 'height': newDims[1] + 'px' });


	$('#preview').hover(function () {  }, onMOut );
	$('#preview').show(300, sideAnimation);

	setImageAfterLoad("previewImage2", "container2", obj.alt);

}

//calculate offsets and animate calibrate popup form posotion if it goes ouside the viewport
function sideAnimation() {
    var scrlTop = $(document).scrollTop();

	var off = $('#preview').offset();
	var offBottom = off.top + $('#preview')[0].scrollHeight;

	var animateLeft = 0;
	var animateTop = 0;
	var animateBottom = 0;

	if (off.left < -40)
	    animateLeft = 0 - off.left;

	if (off.top < scrlTop)
	    animateTop = scrlTop - off.top;

	if (offBottom > $(window).height() + scrlTop)
	    animateBottom = offBottom - $(window).height() - scrlTop;

    if (animateBottom != 0)
	    $('#preview').animate({ marginLeft: '+=' + animateLeft + '', marginTop: '-=' + animateBottom + '' }, 500);
	else
	    $('#preview').animate({ marginLeft: '+=' + animateLeft + '', marginTop: '+=' + animateTop + '' }, 500);
}

//remove popup form 
function onMOut() {
        $('#preview').remove();
    }

//create image size in two variants : width > height and width > height
function initDimensions(width, height, mainSize) {
    var newDim = new Array();
    if (width > height) {
        newDim[0] = mainSize;
        newDim[1] = height * mainSize / width;
    }
    else {
        newDim[1] = mainSize;
        newDim[0] = width * mainSize / height;
    }
    return newDim;
}

function setImageAfterLoad(imgId, aId, src) {
    var loadingImg = document.getElementById(imgId);
    loadingImg.onload = function () {
        $('#' + aId).show();
    }
    loadingImg.src = src;
}

function size(obj) {
    if (obj.width > obj.height)
        $(obj).css({ 'width': '120px' });
    else
        $(obj).css({ 'height': '120px' });
}

function setCookie(name, value) {
    document.cookie = name + "=" + escape(value) + "; path=/; expires=Mon, 02-Jan-2017 00:00:00 GMT";
}

function getCookie(name) {
    var cookie = " " + document.cookie;
    var search = " " + name + "=";
    var setStr = null;
    var offset = 0;
    var end = 0;
    if (cookie.length > 0) {
        offset = cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = cookie.indexOf(";", offset)
            if (end == -1) {
                end = cookie.length;
            }
            setStr = unescape(cookie.substring(offset, end));
        }
    }
    return (setStr);
}


function DelCookie(name) {
    document.cookie = name + "=" + " ;       path=/; expires=Mon, 02-Jan-2005 00:00:00 GMT";
}


function strpos(haystack, needle, offset) {
    var i = haystack.indexOf(needle, offset);
    return i >= 0 ? i : false;
}


function FullClearCookie() {
    allcoockies = document.cookie.substring(0, document.cookie.length) + ';';
    while (allcoockies) {
        var spos = strpos(allcoockies, ';', 0);
        var val = allcoockies.substr(0, spos);
        allcoockies = allcoockies.substr(spos + 2, allcoockies.length);
        coockie_param = val.substr(0, strpos(val, '=', 0));
        if (coockie_param.toUpperCase() != 'PHPSESSID') {
            DelCookie(coockie_param);
        }
    }
}